#include "hud_button.h"

hudButton *hudButton::createHUDButton(node *parent,glm::vec2 pos,const char* name, const char *normalPath, const char *hoverPath,
     const char *pressPath, float scale, Anchor anchor)
{
    auto button = new hudButton();
    button->init();
    button->name_ = name;
    button->setRenderPosition(pos);
    button->sp_normal = sprite::createSprite(button,normalPath, scale, anchor);
    button->sp_hover = sprite::createSprite(button,hoverPath, scale, anchor);
    button->sp_press = sprite::createSprite(button,pressPath, scale, anchor);
    button->sp_hover->setActive(false);
    button->sp_press->setActive(false);
    if(parent) parent->addChild(button);
    return button;
}

bool hudButton::getIsTrigger()
{
    if(isTrigger){
        isTrigger = false;
        isHover = false;
        isPress = false;
        return true;
    }
    return false;
}

bool hudButton::handleEvents(SDL_Event &event)
{
    if(event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT){
        //SDL_Log("button down");
        if(isHover){
            //SDL_Log("isPress");
            isPress = true;
            game_.playSound("assets/sound/UI_button08.wav");
            return true;
        }
    }else if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT){
        //SDL_Log("button up");
        isPress = false;
        if(isHover){
            //TODO: call callback function
            //SDL_Log("isTrigger");
            isTrigger = true;
            return true;
        }
    }
    return false;
}

void hudButton::update(float)
{
    checkHover();
    checkState();
}

void hudButton::checkHover()
{
    bool newHover = false;
    auto pos = render_position + sp_normal->get_offset();
    SDL_Point p = {game_.getMousePosition().x, game_.getMousePosition().y};
    SDL_Rect rect = {static_cast<int>(pos.x), static_cast<int>(pos.y), 
        sp_normal->getSize().x, sp_normal->getSize().y};
    /*if(&p && &rect){
        SDL_Log("pos: %d, %d, rect: %d, %d, %d, %d",
            p.x, p.y, rect.x, rect.y, rect.w, rect.h);
    }*/
    if(SDL_PointInRect(&p, &rect)){
        //SDL_Log("isHover");
        newHover = true;
    }else{
        //SDL_Log("notHover");
        newHover = false;
    }
    if(newHover != isHover){
        isHover = newHover;
        if(isHover && !isPress)
            game_.playSound("assets/sound/UI_button12.wav");
    }
}

void hudButton::checkState()
{
    if(!isPress && !isHover){
        sp_normal->setActive(true);
        sp_hover->setActive(false);
        sp_press->setActive(false);
    }else if(isHover && !isPress){
        sp_normal->setActive(false);
        sp_hover->setActive(true);
        sp_press->setActive(false);
    }else{
        sp_normal->setActive(false);
        sp_hover->setActive(false);
        sp_press->setActive(true);
    }
}

void hudButton::setScale(float scale)
{
    sp_normal->setScale(scale);
    sp_hover->setScale(scale);
    sp_press->setScale(scale);
}
